import Mock from 'mockjs'
import { param2Obj } from '../utils'

// 生成随机消息数据
const messages = Mock.mock({
  'items|30': [{
    'id|+1': 1,
    'title': '@ctitle(6, 20)',
    'content': '@cparagraph(1, 3)',
    'type|1': ['todo', 'process', 'notice', 'announcement'],
    'status|1': ['pending', 'approved', 'rejected', 'warning'],
    'time': '@datetime("yyyy-MM-dd HH:mm:ss")',
    'read': '@boolean',
    'important': '@boolean',
    'sender': {
      'name': '@cname',
      'avatar': 'https://randomuser.me/api/portraits/men/@integer(1, 99).jpg',
      'department': '@ctitle(2, 5)部'
    },
    'path': function() {
      // 根据不同类型分配不同路径
      const type = this.type
      switch (type) {
        case 'todo':
          return '/profile/todo'
        case 'process':
          return '/finance/expense'
        case 'notice':
          return '/profile/notifications'
        case 'announcement':
          return '/profile/announcements'
        default:
          return '/'
      }
    }
  }]
})

export default {
  // 获取消息列表
  getMessages: config => {
    const { type, status, page = 1, limit = 10 } = param2Obj(config.url)
    
    let mockList = messages.items.filter(item => {
      if (type && type !== 'all' && item.type !== type) return false
      if (status && item.status !== status) return false
      return true
    })
    
    const pageList = mockList.filter((item, index) => {
      return index < limit * page && index >= limit * (page - 1)
    })
    
    return {
      code: 200,
      data: {
        total: mockList.length,
        list: pageList,
        page: Number(page),
        limit: Number(limit)
      }
    }
  },
  
  // 获取未读消息数量
  getUnreadCount: () => {
    const unreadCount = messages.items.filter(item => !item.read).length
    
    return {
      code: 200,
      data: unreadCount
    }
  },
  
  // 标记消息为已读
  markAsRead: config => {
    const { id } = param2Obj(config.url)
    const messageIndex = messages.items.findIndex(item => item.id === Number(id))
    
    if (messageIndex > -1) {
      messages.items[messageIndex].read = true
      return {
        code: 200,
        data: 'success'
      }
    } else {
      return {
        code: 404,
        message: '消息不存在'
      }
    }
  },
  
  // 批量标记已读
  batchMarkAsRead: config => {
    const { ids } = JSON.parse(config.body)
    
    if (Array.isArray(ids) && ids.length > 0) {
      ids.forEach(id => {
        const messageIndex = messages.items.findIndex(item => item.id === Number(id))
        if (messageIndex > -1) {
          messages.items[messageIndex].read = true
        }
      })
      
      return {
        code: 200,
        data: 'success'
      }
    } else {
      return {
        code: 400,
        message: '参数错误'
      }
    }
  },
  
  // 标记所有消息为已读
  markAllRead: config => {
    const { type } = param2Obj(config.url)
    
    if (type && type !== 'all') {
      messages.items.forEach(item => {
        if (item.type === type) {
          item.read = true
        }
      })
    } else {
      messages.items.forEach(item => {
        item.read = true
      })
    }
    
    return {
      code: 200,
      data: 'success'
    }
  },
  
  // 删除消息
  deleteMessage: config => {
    const { id } = param2Obj(config.url)
    const messageIndex = messages.items.findIndex(item => item.id === Number(id))
    
    if (messageIndex > -1) {
      messages.items.splice(messageIndex, 1)
      return {
        code: 200,
        data: 'success'
      }
    } else {
      return {
        code: 404,
        message: '消息不存在'
      }
    }
  },
  
  // 批量删除消息
  batchDeleteMessages: config => {
    const { ids } = JSON.parse(config.body)
    
    if (Array.isArray(ids) && ids.length > 0) {
      const remainingMessages = messages.items.filter(item => !ids.includes(item.id))
      messages.items.length = 0
      messages.items.push(...remainingMessages)
      
      return {
        code: 200,
        data: 'success'
      }
    } else {
      return {
        code: 400,
        message: '参数错误'
      }
    }
  },
  
  // 获取消息详情
  getMessageDetail: config => {
    const { id } = param2Obj(config.url)
    const message = messages.items.find(item => item.id === Number(id))
    
    if (message) {
      return {
        code: 200,
        data: message
      }
    } else {
      return {
        code: 404,
        message: '消息不存在'
      }
    }
  }
} 